package com.acaomei.skype.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

public class SqlInsulateUtil {
	
	private static final ThreadLocal<String> USER = new ThreadLocal<String>() {
        @Override
        protected String initialValue() {
            return null;
        }
    };
    
    // 是否可读取到文件
    private static boolean isReadInsulateMark = true;
    
    public static List<String> insulateMarkList = null;
    
    public static List<String> getInsulateMarkList() throws IOException{
    	if(!isReadInsulateMark) {
    		return null;
    	}
    	
    	if(insulateMarkList != null) {
    		return insulateMarkList;
    	}
    	
    	Properties properties = new Properties();
        // 使用ClassLoader加载properties配置文件生成对应的输入流
        InputStream in =SqlInsulateUtil.class.getClassLoader().getResourceAsStream("config/insulateMark.properties");
        if(in == null) {
        	isReadInsulateMark = false;
        	return null;
        }
        // 使用properties对象加载输入流
        properties.load(in);
        //获取key对应的value值
        String insulationList = properties.getProperty("insulationList");
        if(insulationList == null) {
        	isReadInsulateMark = false;
        	return null;
        }
        insulateMarkList = Arrays.asList(insulationList.split(","));
		return insulateMarkList;
    }
    
    /**
     * 获取map中第一个key值
     * @param map 数据源
     * @return 
     */
    public static String getKeyOrNull(Map<String, Object> map) {
    	String obj = null;
        for (Entry<String, Object> entry : map.entrySet()) {
            obj = entry.getKey();
            if (obj != null) {
                break;
            }
        }
        return  obj;
    }
    
    /**
     * 获取map中第一个数据值
     * @param map 数据源
     * @return 
     */
    public static Object getFirstOrNull(Map<String, Object> map) {
    	Object obj = null;
        for (Entry<String, Object> entry : map.entrySet()) {
            obj = entry.getValue();
            if (obj != null) {
                break;
            }
        }
        return  obj;
    }
    
    public static String getUser() {
        return USER.get();
    }

    public static void setUser(String user) {
    	USER.set(user);
    }
    
    /**
     * 移除本地变量
     */
    public static void clearUser() {
        USER.remove();
    }
    
    /**
     * 测试标志枚举
     * @author Administrator
     */
    public static enum TestSignEnum {
    	TEST(1,"是测试数据"), 
    	NOT_TEST(0,"不是测试数据");
		
		private Integer code;
		
		private String remark;
		
		private TestSignEnum(Integer code, String remark){
			this.code = code;
			this.remark = remark;
		}
		
		public Integer getCode() {
			return code;
		}
		
		public void setCode(Integer code) {
			this.code = code;
		}
		
		public String getRemark() {
			return remark;
		}
		
		public void setRemark(String remark) {
			this.remark = remark;
		}
    }
    
}
